<template>
  <div>
    <div v-if="!hasError" id="loader-wrapper">
      <div id="loader"></div>
      <div class="loader-section section-left"></div>
      <div class="loader-section section-right"></div>
      <div class="load_title">{{ info }}</div>
    </div>
    <el-button v-if="hasError" @click="logout">重新登陆</el-button>
  </div>
</template>
<script>
export default {
  created() {},

  mounted() {
    const { code, target_uri } = this.$route.query;
    this.target_uri = target_uri;
    this.code = code;
    if (code) {
      this.$store
        .dispatch("ssologin", code)
        .then(res => {
          console.log(res);
          if (res.success) {
            this.info = "登陆成功,系统资源加载中...";
            this.$router.push({ path: target_uri || "/" }).catch(() => {});
          } else {
            this.hasError = true;
          }
        })
        .catch(() => {
          this.hasError = true;
          this.info = "登陆过程出现问题,请重试！";
        })
        .finally(n => {});
    } else {
      alert("code缺失");
    }
  },

  methods: {
    logout() {
      this.$store.dispatch("LogOut", this.target_uri);
      setTimeout(() => {}, 1000);
    }
  },
  data() {
    return {
      target_uri: "",
      code: "",
      info: "跳转中...",
      hasError: false
    };
  }
};
</script>
